﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Security.Permissions;
using System.Web;

namespace bp.BusinessLogic
{
    public class Mail
    {
        public string SendMail(string messageFile, string mailServer, string sender, string recepient, string authenticationUser, string authenticationUserPassword, string subject, string firstName, string LastName, string messageContent, Guid guid,  string tel, string interest, string language )
        {
            // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0

            // System.Net.Mail.SmtpClient is the alternate class for this in 2.0



            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();

            try
            {
                FileIOPermission filePerm = new FileIOPermission(FileIOPermissionAccess.Read, messageFile);
                filePerm.Assert();

                smtpClient.Host = mailServer; 
                smtpClient.Port = 25;
                message.From = new MailAddress(sender);
                message.To.Add(recepient);
                message.ReplyTo = new MailAddress(sender);
                message.Subject = subject;
                NetworkCredential SMTPUserInfo = new NetworkCredential(authenticationUser, authenticationUserPassword);
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = SMTPUserInfo;
                message.Priority = MailPriority.Normal;
                smtpClient.EnableSsl = false;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;


                // CC and BCC optional

                // MailAddressCollection class is used to send the email to various users

                // You can specify Address as new MailAddress("admin1@yoursite.com")

                //message.CC.Add("admin1@yoursite.com");
                //message.CC.Add("admin2@yoursite.com");

                // You can specify Address directly as string

                //message.Bcc.Add(new MailAddress("admin3@yoursite.com"));
                //message.Bcc.Add(new MailAddress("admin4@yoursite.com"));

                //Body can be Html or text format

                //Specify true if it  is html message

                message.IsBodyHtml = true;

                // Message body content

                StreamReader sr = new StreamReader(messageFile);


                message.Body = sr.ReadToEnd().Replace("{FirstName}", firstName).Replace("{LastName}", LastName).Replace("{Message}", messageContent).Replace("{Guid}", guid.ToString()).Replace("{Email}", sender).Replace("{Tel}", tel).Replace("{Interest}", interest).Replace("{Language}", language).Replace("{Receipient}", recepient);

                // Send SMTP mail

                smtpClient.Send(message);

                return  "Email successfully sent.";
            }
            catch (Exception ex)
            {
                string exmessage = "";
                ConcatenateError(ex, ref exmessage);
                return "Send Email Failed." + exmessage;
            }
        }

        private static void ConcatenateError(Exception ex, ref string Message)
        {
            Message += ex.Message;
            if (ex.InnerException != null)
            {
                ConcatenateError(ex.InnerException, ref Message);
            }
        }
    }
}
